package team.seekers.catering.food.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import team.seekers.catering.common.result.R;
import team.seekers.catering.common.result.ResultCode;
import team.seekers.catering.food.entity.dto.add.FoodAddDTO;
import team.seekers.catering.food.entity.dto.update.FoodUpdateDTO;
import team.seekers.catering.food.entity.dto.query.FoodQuery;
import team.seekers.catering.food.entity.vo.info.FoodInfoVO;
import team.seekers.catering.food.service.FoodService;
import team.seekers.catering.food.service.IngredientService;

import java.util.Map;

/**
 * <p>
 * 食物表 前端控制器
 * </p>
 *
 * @author hbc
 * @since 2022-02-04
 */
@RestController
@RequestMapping("/food")
public class FoodController {

    @Autowired
    private FoodService foodService;
    @Autowired
    private IngredientService ingredientService;


    /**
     * 新增单品
     */
    @PostMapping("/add")
    public R add(@RequestBody FoodAddDTO foodAddDTO){
        ingredientService.setParamFromList(foodAddDTO);
        String id = foodService.add(foodAddDTO);
        return R.success().data("id",id);
    }

    /**
     * 删除单品
     */
    @GetMapping("/delete/{foodId}")
    public R delete(@PathVariable("foodId")String id){
        foodService.removeById(id);
        return R.success();
    }

    /**
     * 修改单品
     */
    @PostMapping("/update")
    public R update(@RequestBody FoodUpdateDTO foodUpdateDTO){
        ingredientService.setParamFromList(foodUpdateDTO);
        String id = foodService.update(foodUpdateDTO);
        return R.success().data("id",id);
    }

    /**
     * 单品分页查询
     */
    @PostMapping("/adminPageOrder/{current}/{limit}")
    public R adminPageOrder(@PathVariable("current") long current,
                            @PathVariable("limit") long limit,
                            @RequestBody FoodQuery query){
        Map<String, Object> map = foodService.adminPageOrder(current,limit,query);
        if(map == null){
            return R.fail(ResultCode.GET_FAIL);
        }
        return R.success(map);
    }

    /**
     * 单品详情
     */
    @GetMapping("/query/{foodId}")
    public R query(@PathVariable("foodId")String id){
        FoodInfoVO foodInfoVO = foodService.get(id);
        return R.success().data("info",foodInfoVO);
    }
}

